package ncku.account.event.old;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;

import ncku.account.*;
import ncku.account.data.Category;
import ncku.account.data.GlobalData;
import ncku.account.data.SubTransaction;
import ncku.account.data.SubTransactionList;
import ncku.account.data.Transaction;

public class EditTransaction {
	public SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
	public DateFormat df = DateFormat.getDateInstance();
	Scanner scanner = new Scanner(System.in);
	
	int chooseIndex=0;  //選擇修改之Transaction index
	int range = GlobalData.getTransactionNumber();  //取得Transaction總數
	
	Date newDate;
	String newDescription;
	int newAmount;
	int oldAmount;
	Category newCategory;
	private SubTransactionList newSubTransList;  //subTransaction List Object
	
	public EditTransaction() throws Exception{
		newSubTransList = new SubTransactionList();  //新增Transaction時,就先new a subTrasanction List Object
		chooseIndex = ChooseTransaction();
		getInfo(chooseIndex);
		GlobalData.UpdateBalanceMoney((-1*oldAmount));  //先把舊帳的金額加回來
		
		ArrayList<SubTransaction> newSubTransaction;
		newSubTransaction = GlobalData.checkSumOfAmount(newAmount,newSubTransList);
		GlobalData.edit(chooseIndex, newDate, newDescription, newAmount, newCategory, newSubTransaction);
	}
    
    public int ChooseTransaction(){
    	int input;  //輸入的暫存器
    	listTransaction show = new listTransaction();
    	show.listTransaction();  //輸出所有Transaction
    	
    	while(true){
    		System.out.println("請輸入對應Transaction序號:");
    		input = Integer.valueOf(scanner.next());
    		
    		if((input>0) && (input<=range)){  //若輸入之index值符合範圍內(0~range)，即跳出此迴圈；若否則請使用者繼續輸入
    			return (input-1);
    		}
    		else{
    			System.out.println("輸入之序號錯誤!");
    		}
    	}
    }
    
    public void getInfo(int index) throws Exception{
    	//先取得原Transaction的資料(Date,description,amount,category
    	
    	String temp;
    	String date = GlobalData.getTransaction(index).getDate();
    	String description = GlobalData.getTransaction(index).getDescription();
    	String amount = GlobalData.getTransaction(index).getStringAmount();
    	newCategory = new Category(GlobalData.getCategory(index).getName());
    	ArrayList<SubTransaction> subTransaction = GlobalData.getTransaction(index).getAllSubTrans();
    	newSubTransList.subTransactions = subTransaction;
    	
    	oldAmount = GlobalData.getTransaction(index).getAmount();
    	
    	String inputDate = date;
    	String inputDescription = description;
    	String inputAmount = amount;

    	System.out.println("請輸入該Transation欲修改的部分,若此項目不修改則輸入(N)");
    	System.out.println("Date:");
    	temp = scanner.next();
    	if(!temp.equals("N")){
    		inputDate = temp;
    		temp = "";
    	}
    	
    	System.out.println("Description:");
    	temp = scanner.next();
    	if(!temp.equals("N")){
    		inputDescription = temp;
    		temp = "";
    	}
    	
    	System.out.println("Category:");
    	temp = scanner.next();
    	if(!temp.equals("N")){
    		newCategory = new Category(temp);
    		temp = "";
    	}
    	
    	System.out.print("是否新增SubTransaction:(Y/N)");
		temp = scanner.next();
		
		if(temp.equals("Y")){  
			System.out.println("若不輸入SubTransaction資料,請打N");
			getSubTransactioInfo();
		}
		
		System.out.println("Edit SubTransaction:");
    	temp = scanner.next();
    	if(!temp.equals("N")){
    		EditSubTransaction();
    		temp = "";
    	}

    	System.out.println("Delete SubTransaction:");
    	temp = scanner.next();
    	if(!temp.equals("N")){
    		DeleteSubTransaction();
    		temp = "";
    	}
    	
    	System.out.println("Amount:");
    	temp = scanner.next();
    	if(!temp.equals("N")){
    		inputAmount = temp;
    		temp = "";
    	}
    	    	
    	if(!inputDate.equals(date)){
    		date = inputDate;
    	}
    	if(!inputDescription.equals(description)){
    		description = inputDescription;
    	}
    	if(!inputAmount.equals(amount)){
    		amount = inputAmount;
    	}
    	
    	newDate = df.parse(date);
    	newDescription = description;
    	newAmount = Integer.valueOf(amount);
    	
    }
    
    public void getSubTransactioInfo(){
		String subDescription;
		int subAmount = 0;
		
		boolean conti = true;
		String temp ="";
		 
		while(true){
			System.out.print("SubTransaction description:(N)");
			temp = scanner.next();
			if(temp.equals("N")){
				break;
			}
			else{
				subDescription = temp;
				while(conti){
					System.out.print("SubTransaction amount:(N)");
					temp = scanner.next();
					if(!temp.equals("N")){
						subAmount = Integer.valueOf(temp);
						conti = false;
					}
					else{
						conti = false;
					}
				}
				//取得description amount等資料,並將資料加入ArrayList SubTransaction中
				newSubTransList.addSubTrans(subDescription, subAmount);
				UpdateNowTransAmount(subAmount);
				conti = true;
			}
		}
	}
    
    public void UpdateNowTransAmount(int subAmount){
    	newAmount += subAmount;
	}
    
    public void EditSubTransaction(){
    	System.out.println("Index:");
    	int index = Integer.valueOf(scanner.next());
    	System.out.println("Description:");
    	String description= scanner.next();
    	System.out.println("Amount:");
    	int amount = Integer.valueOf(scanner.next());
    	
    	newSubTransList.editSubTrans(index, description, amount);
    }
    
    public void DeleteSubTransaction(){
    	System.out.println("Index:");
    	int index = Integer.valueOf(scanner.next());
    	newSubTransList.deleteSubTrans(index);
    }
}
